package com.gomore.opple.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.service.CancelClickListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Company 上海勾芒科技有限公司.
 */
public class CustomProgressDialog extends Dialog {
    private CancelClickListener cancelClickListener;
    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.init_spinny)
    ProgressBar progressBar;
    @Bind(R.id.tv_progress_precent)
    TextView tv_progress_precent;
    @Bind(R.id.tv_progress_total)
    TextView tv_progress_total;

    @Bind(R.id.btn_cancel)
    Button btn_cancel;

    private String title = "";
    private int mMax = 100;
    private int progress = 0;
    private boolean isVisiable;

    private final String PRECENT_FORMATE = "%d%%";
    private final String TOTAL_FORMATE = "%.2fMB/%.2fMB";

    private final int UNIT_SIZE = 1024;

    public void setCancelClickListener(CancelClickListener cancelClickListener) {
        this.cancelClickListener = cancelClickListener;
    }

    private Handler updateViewHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            updateView();
        }
    };

    public CustomProgressDialog(Context context, boolean isVisiable) {
        super(context);
        this.isVisiable = isVisiable;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.progress_dialog_loading);
        ButterKnife.bind(this);
        //按空白处不能取消对话框
        setCanceledOnTouchOutside(false);
        if (isVisiable) {
            btn_cancel.setVisibility(View.VISIBLE);
        } else {
            btn_cancel.setVisibility(View.GONE);
        }
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelClickListener.cancel();
                dismiss();
            }
        });
        updateView();
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void updateView() {
        try {
            progressBar.setMax(mMax);
            progressBar.setProgress(progress);
            float pro = (float) progress / mMax;
            float proStr = progress / (float) (UNIT_SIZE * UNIT_SIZE);
            float totalStr = mMax / (float) (UNIT_SIZE * UNIT_SIZE);
            tv_progress_precent.setText(String.format(PRECENT_FORMATE, (int) (pro * 100)));
            tv_title.setText(title);
            tv_progress_total.setText(String.format(TOTAL_FORMATE, proStr, totalStr));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setmMax(int mMax) {
        this.mMax = mMax;
        postValid();
    }

    public void setTitle(String title) {
        this.title = title;
        postValid();
    }

    public void setProgress(Integer progress, int total) {
        this.mMax = total;
        if (progress < 0 || progress > mMax) {
            return;
        }
        this.progress = progress;
        postValid();
    }

    private void postValid() {
        updateViewHandler.sendEmptyMessage(0);
    }
}
